//
//  RXEvent.m
//  iMed
//
//  Created by Barry Press on 8/14/09.
//  Copyright 2009 Barry Press. All rights reserved.
//

#import "RXEvent.h"
#import "Constants.h"
#import "TimeIntervalFormatter.h"
#import "History.h"
#import "DataSet.h"
#import "MyDocument.h"

@implementation RXEvent

// =============================================================================
// ---------------------------------------------- description ------------------
- (NSString*)description
{
	NSDateFormatter* fmt = [[[NSDateFormatter alloc] init] autorelease];
	[fmt setDateStyle:NSDateFormatterNoStyle];
	[fmt setTimeStyle:NSDateFormatterShortStyle];
	return [NSString stringWithFormat:@"%@\t%@", 
			rx.ndc.name,
			[fmt stringFromDate:[self timestamp]]];
}

#pragma mark Application methods
// =============================================================================
// ---------------------------------------------- populateRXEvent --------------
- (void)populateRXEventUsingDataSet:(DataSet*) ds
{
//	Fill in values for a new RXEvent
	NSTimeInterval rxInterval = [rx interval];
	int iCount;
	History* h;
	NSDate* nextDate;
	
	// Get the average interval for this specific RX over all the elements
	// presently in the history log.
	NSTimeInterval avg = [ds averageIntervalForRX:rx 
									 returnsCount:&iCount 
								   returnsHistory:&h];
//	NSTimeInterval intv = fmod(fmax(rxInterval * (iCount + 1) - avg * iCount, 0.0),
//							   ONEDAY);
	NSTimeInterval intv = fmax(rxInterval * (iCount + 1) - avg * iCount, 0.0);
	[self setInterval:intv];
	[self setDayCount:iCount];
	if (h != NULL) {
		nextDate = [[h timestamp] dateByAddingTimeInterval:intv];
	}
	else {
		nextDate = [[NSDate date] dateByAddingTimeInterval:rxInterval];
	}
	
	[self setTimestamp:nextDate];
}

// ---------------------------------------------- updateInterval ---------------
- (NSTimeInterval)updateInterval
{
	NSTimeInterval i = [[self timestamp] timeIntervalSinceDate:[NSDate date]];
	[self setInterval:i];
	return i;
}

#pragma mark Printed output methods
// ----------------------------------------------  --------
- (void)textOutputEventToView:(NSView*)view
{
	NSDateFormatter* fmt = [[[NSDateFormatter alloc] init] autorelease];
	[fmt setDateStyle:NSDateFormatterNoStyle];
	[fmt setTimeStyle:NSDateFormatterShortStyle];
	
	NSDate* limit = [[NSDate date] dateByAddingTimeInterval:ONEDAY];
	[view insertText:self.rx.ndc.name];
	NSTimeInterval i = [[self rx] interval];
	NSDate* next = [self timestamp];
	while (i > 0 && [next isLessThan:limit]) {
		[view insertText:[NSString stringWithFormat:@"\t%@", [fmt stringFromDate:next]]];
		next = [next dateByAddingTimeInterval:i];
	}
	[view insertText:@"\n"];
}

#pragma mark Archiving methods
// =============================================================================
// ---------------------------------------------- dealloc ----------------------
- (void)dealloc
{
	[timestamp release];
	timestamp = NULL;
	[rx release];
	rx = NULL;
	[super dealloc];
}

// ---------------------------------------------- encodeWithCoder --------------
- (void)encodeWithCoder:(NSCoder *)coder
{
	[coder encodeObject:timestamp forKey:@"RXEtimestamp"];
	[coder encodeObject:rx forKey:@"RXErx"];
	[coder encodeFloat:rxcount forKey:@"RXErxcount"];
	[coder encodeFloat:dose forKey:@"RXEdose"];
	[coder encodeFloat:interval forKey:@"RXEInterval"];
	[coder encodeInt:dayCount forKey:@"RXEdayCount"];
}

// ---------------------------------------------- init -------------------------
- (id)init
{
	[super init];
	timestamp = nil;
	rx = NULL;
	rxcount = 0.0;
	dose = 1.0;
	return self;
}

// ---------------------------------------------- initWithCoder ----------------
- (id)initWithCoder:(NSCoder *)coder
{
	[super init];
	timestamp = [[coder decodeObjectForKey:@"RXEtimestamp"] retain];
	rx = [[coder decodeObjectForKey:@"RXErx"] retain];
	rxcount = [coder decodeFloatForKey:@"RXErxcount"];
	dose = [coder decodeFloatForKey:@"RXEdose"];
	interval = [coder decodeFloatForKey:@"RXEInterval"];
dayCount = [coder decodeIntForKey:@"RXEdayCount"];
	return self;
}

#pragma mark Accessor methods

// ---------------------------------------------- accessors --------------------
@synthesize timestamp;
@synthesize rx;
@synthesize dose;
@synthesize rxcount;
@synthesize interval;
@synthesize dayCount;

@end
